<?php
namespace app\index\service;


use app\index\model\AcckuanModel;
use app\index\model\AccountDiaoModel;
use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondShouModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeHuanModel;
use app\index\model\IncomeQiModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;

class FinanceService extends CommonService
{

    public function check_dealcom_name($name, $id=0){
        return !!DealcomModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0,'status'=>1,'isturn'=>1])->where('id','neq', $id)->find();
    }

    public function create_account_list_jiaonum_shou(){
        $tou = 'SR';
        $daystr = substr(date("Ymd"), 2);
        $number = AccountListModel::where('jiaonum','like','%'.$tou.$daystr.'%')->order('jiaonum DESC')->value('jiaonum');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }
    public function create_account_list_jiaonum_zhi(){
        $tou = 'ZC';
        $daystr = substr(date("Ymd"), 2);
        $number = AccountListModel::where('jiaonum','like','%'.$tou.$daystr.'%')->order('jiaonum DESC')->value('jiaonum');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function create_account_diao_number(){
        $tou = 'ZJDB';
        $daystr = substr(date("Ymd"), 2);
        $number = AccountDiaoModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }









    public function create_account_diao_shou($mid, $data){
        $ins = [];
        $ins['comid'] = $this->userinfo['comid'];
        $ins['type'] = 11;
        $ins['mid'] = $mid;
        $ins['jiaonum'] = $this->create_account_list_jiaonum_shou();
        $ins['money'] = $data['money'];
        $ins['budid'] = $data['rubudid'];
        $ins['zhai'] = $data['zhai'];

        $acc_chanid = AccountModel::where(['id'=>$data['chuid']])->value('chanid');
        $ins['faer'] = DeptModel::get_name_by_pk($acc_chanid);

        $ins['daotype'] = '转账';
        $ins['dt'] = $data['dt'];
        $ins['accid'] = $data['ruid'];
        $ins['baner'] = $data['baner'];
        $ins['created_user'] = $this->userinfo['id'];
        $ins['updated_user'] = $this->userinfo['id'];
        $ins['created_at'] = date("Y-m-d H:i:s");
        $ins['updated_at'] = date("Y-m-d H:i:s");

        AccountListModel::insert($ins);
        return true;
    }
    public function create_account_diao_zhi($mid, $data){
        $ins = [];
        $ins['comid'] = $this->userinfo['comid'];
        $ins['type'] = 12;
        $ins['mid'] = $mid;
        $ins['jiaonum'] = $this->create_account_list_jiaonum_zhi();
        $ins['money'] = $data['money'];
        $ins['budid'] = $data['chubudid'];
        $ins['zhai'] = $data['zhai'];

        $acc_chanid = AccountModel::where(['id'=>$data['ruid']])->value('chanid');
        $ins['faer'] = DeptModel::get_name_by_pk($acc_chanid);

        $ins['daotype'] = '转账';
        $ins['dt'] = $data['dt'];
        $ins['accid'] = $data['chuid'];
        $ins['baner'] = $data['baner'];
        $ins['created_user'] = $this->userinfo['id'];
        $ins['updated_user'] = $this->userinfo['id'];
        $ins['created_at'] = date("Y-m-d H:i:s");
        $ins['updated_at'] = date("Y-m-d H:i:s");

        AccountListModel::insert($ins);
        return true;
    }
    public function update_account_diao_shou($mid, $data){
        $upd = [];
        $upd['money'] = $data['money'];
        $upd['budid'] = $data['rubudid'];
        $upd['zhai'] = $data['zhai'];
        $upd['dt'] = $data['dt'];
        $upd['baner'] = $data['baner'];
        $upd['updated_user'] = $this->userinfo['id'];
        $upd['updated_at'] = date("Y-m-d H:i:s");
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>11,'mid'=>$mid])->update($upd);
        return true;
    }
    public function update_account_diao_zhi($mid, $data){
        $upd = [];
        $upd['money'] = $data['money'];
        $upd['budid'] = $data['chubudid'];
        $upd['zhai'] = $data['zhai'];
        $upd['dt'] = $data['dt'];
        $upd['baner'] = $data['baner'];
        $upd['updated_user'] = $this->userinfo['id'];
        $upd['updated_at'] = date("Y-m-d H:i:s");
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>12,'mid'=>$mid])->update($upd);
        return true;
    }
    public function del_account_diao_shou($mid){
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>11,'mid'=>$mid])->delete();
        return true;
    }
    public function del_account_diao_zhi($mid){
        AccountListModel::where(['comid'=>$this->userinfo['comid'],'type'=>12,'mid'=>$mid])->delete();
        return true;
    }



    public function get_account_list_type(){
        //交易类别
        // 1-项目收款 2-其他收款 3-项目付款 4-项目费用报销 5-日常付款 6-项目内包付款 7-保证金缴纳 8-保证金退回
        // 9-保证金收取 10-保证金返还 11-资金调拨收 12-资金调拨支 13-备用金申请 14-还款申请(备用金) 15-公司费用报销
        return [
            ['name' => '项目收款', 'type'=> 1, 'form' => 1],
            ['name' => '其他收款', 'type'=> 2, 'form' => 1],
            ['name' => '项目付款', 'type'=> 3, 'form' => 2],
            ['name' => '项目费用报销', 'type'=> 4, 'form' => 2],
            ['name' => '日常付款', 'type'=> 5, 'form' => 2],
            ['name' => '项目内包付款', 'type'=> 6, 'form' => 2],
            ['name' => '保证金缴纳', 'type'=> 7, 'form' => 2],
            ['name' => '保证金退回', 'type'=> 8, 'form' => 1],
            ['name' => '保证金收取', 'type'=> 9, 'form' => 1],
            ['name' => '保证金返还', 'type'=> 10, 'form' => 2],
            ['name' => '资金调拨收', 'type'=> 11, 'form' => 1],
            ['name' => '资金调拨支', 'type'=> 12, 'form' => 2],
            ['name' => '备用金申请', 'type'=> 13, 'form' => 2],
            ['name' => '还款申请(备用金)', 'type'=> 14, 'form' => 1],
            ['name' => '公司费用报销', 'type'=> 15, 'form' => 2],
        ];
    }

    public function get_account_list_type_name($type){
        $str = '';
        $arr = $this->get_account_list_type();
        foreach ($arr as $v){
            if($v['type'] == $type){
                $str = $v['name'];
                break;
            }
        }
        return $str;
    }

    public function get_account_list_midinfo($type, $mid){
        $data['mid'] = $mid;
        $data['mid_page'] = '';
        $data['mid_number'] = '';


        if($type == 1){
            $info = IncomeEnginModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'income_engin';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 2){
            $info = IncomeQiModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'income_qi';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 3){
            $info = PaymentEnginModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'payment_engin';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 4){
            $info = PaymentBaoModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'payment_bao';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 5){
            $info = PaymentDayModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'payment_day';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 6){
            $info = PaymentNeifuModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'payment_neifu';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 7){
            $info = EnginBondJiaoModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'engin_bond_jiao';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 8){
            $info = EnginBondJiaoModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'engin_bond_jiao';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 9){
            $info = EnginBondShouModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'engin_bond_shou';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 10){
            $info = EnginBondFanModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'engin_bond_fan';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 11){
            $info = AccountDiaoModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'account_diao';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 12){
            $info = AccountDiaoModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'account_diao';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 13){
            $info = PaymentJieModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'payment_jie';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 14){
            $info = IncomeHuanModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'income_huan';
                $data['mid_number'] = $info['number'];
            }
        }elseif($type == 15){
            $info = PaymentGongModel::where(['id'=>$mid])->find();
            if($info){
                $data['mid_page'] = 'payment_gong';
                $data['mid_number'] = $info['number'];
            }
        }

        return $data;
    }




    public function get_acckuan_info_bybillid($billid, $listid=0){
        $kuaninfo = AcckuanModel::where(['billid'=>$billid,'listid'=>$listid])->order('modtype DESC')->find();
        if($kuaninfo){
            $kuaninfo['paystate_name'] = (new KuanService())->get_acckuan_paystate_name($kuaninfo['paystate']);
            $kuaninfo['paylastdt'] = AccountListModel::where(['kuanid'=>$kuaninfo['id']])->order('dt DESC')->value('dt');
            $kuaninfo['paynofee'] = round($kuaninfo['money'] - $kuaninfo['payfee'], 2);
        }
        return $kuaninfo;
    }















}

